package com.hxzy.common.aspect;

import com.hxzy.common.annotates.RequiredRole;
import com.hxzy.common.constants.MarketConstants;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.exception.ServiceException;
import com.hxzy.common.util.MarketThreadLocalUtil;
import com.hxzy.controller.admin.login.resp.AdminLoginResp;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Spring aop的切面增强类，依赖 spring-boot-starter-aop.jar
 */
@Component
@Aspect
public class RequiredRoleAspect {

    //1、定义切入点（查询哪些类中的哪些方法），5种增强接口 @Before  @After  @AfterThrowing  @AfterReturing @Around
    @Pointcut("@annotation(com.hxzy.common.annotates.RequiredRole)")
    private void requireRoleExpression() {
    }


    /**
     * 定义增强的功能
     */
    @Before(value = "requireRoleExpression() && @annotation(requiredRole)")
    public void beforeCheckRole(RequiredRole requiredRole) {
        AdminLoginResp adminLoginResp = MarketThreadLocalUtil.ADMIN_THREAD_LOCAL.get();
        if(! requiredRole.role().equals(adminLoginResp.getPermissions())   ){
            throw new ServiceException(AckCode.PERMISSION_NOT_ACCESS);
        }
    }


}
